﻿//<TunynetCopyright>
//--------------------------------------------------------------
//<copyright>青岛拓宇网络科技有限公司 2005-2008</copyright>
//<version>V0.5</verion>
//<createdate>2008-3-15</createdate>
//<author>mazq</author>
//<email>mazq@Tunynet.com</email>
//<log date="2008-3-15">创建</log>
//--------------------------------------------------------------
//</TunynetCopyright>


using System;
using System.Collections.Generic;
using System.Text;
using Tunynet.QingdaoCustoms.Common;
using ChinaCustoms.Qingdao.Workflow.Permissions;
using ChinaCustoms.Framework.DeluxeWorks.Library.OGUPermission;
using System.Collections.ObjectModel;

namespace ChinaCustoms.Qingdao.Workflow.Supports
{
    /// <summary>
    /// 子任务业务逻辑类
    /// </summary>
    public class SubTasks
    {

        /// <summary>
        /// 创建子任务
        /// </summary>
        /// <param name="subTask">需创建的子任务</param>
        public static void CreateSubTask(SubTaskInfo subTask)
        {
            BusinessDataProvider.Instance().CreateUpdateSubTask(subTask, DataProviderAction.Create);
        }

        /// <summary>
        /// 更新子任务
        /// </summary>
        /// <param name="subTask">需要更新的子任务</param>
        public static void UpdateSubTask(SubTaskInfo subTask)
        {
            BusinessDataProvider.Instance().CreateUpdateSubTask(subTask, DataProviderAction.Update);
        }

        public static void UpdateSubTaskAcceptDate(Guid subTaskGuid,DateTime acceptDate)
        {
            BusinessDataProvider.Instance().UpdateSubTaskAcceptDate(subTaskGuid, acceptDate);
        }

        ///// <summary>
        ///// SubFlow拆分的SubTask是否已经完成
        ///// </summary>
        ///// <param name="subFlowGuid">子任务ID</param>
        ///// <param name="excludeSubTaskGuid">排除SubTaskGuid=excludeSubTaskGuid后，SubFlow是否已经完成</param>
        //public static bool IsSubFlowComplete(Guid subFlowGuid, Guid excludeSubTaskGuid)
        //{
        //    return BusinessDataProvider.Instance().IsSubFlowComplete(subFlowGuid, excludeSubTaskGuid);
        //}

        /// <summary>
        /// SubTask是否已经完成(或即将完成)
        /// </summary>
        /// <remarks>所有子SubTask完成即SubTask完成</remarks>
        /// <param name="subTaskGuid">子任务ID</param>
        /// <param name="childSubFlowGuid">childSubFlowGuid</param>
        /// <param name="excludeWorkItemGuid">排除WorkItemGuid=excludeWorkItemGuid后，SubTask是否即将完成</param>
        public static bool IsSubTaskComplete(Guid subTaskGuid,Guid taskGuid, Guid childSubFlowGuid, Guid excludeWorkItemGuid)
        {
            return BusinessDataProvider.Instance().IsSubTaskComplete(subTaskGuid,taskGuid, childSubFlowGuid, excludeWorkItemGuid);
        }

        /// <summary>
        /// 尝试完成子任务
        /// </summary>
        /// <param name="subTaskGuid">子任务GUID</param>
        public static void TryCompleteSubTask(Guid subTaskGuid)
        {
            BusinessDataProvider.Instance().TryCompleteSubTask(subTaskGuid);
        }

        /// <summary>
        /// 强制完成子任务
        /// </summary>
        /// <param name="subTaskGuid">子任务GUID</param>
        public static void CompleteSubTask(Guid subTaskGuid)
        {
            BusinessDataProvider.Instance().CompleteSubTask(subTaskGuid);
        }


        /// <summary>
        /// 获取SubTask
        /// </summary>
        /// <param name="subTaskGuid">subTaskGuid</param>
        /// <returns>SubTaskInfo</returns>
        public static SubTaskInfo GetSubTask(Guid subTaskGuid)
        {
            return BusinessDataProvider.Instance().GetSubTask(subTaskGuid);
        }

        /// <summary>
        /// 根据条件获取SubTask
        /// </summary>
        /// <param name="activityGuid">活动ID</param>
        /// <param name="status">SubTaskStatus</param>
        /// <returns> SubTaskInfo集合 </returns>
        public static List<SubTaskInfo> GetSubTasks(Guid activityGuid, SubTaskStatus? status)
        {
            return BusinessDataProvider.Instance().GetSubTasks(activityGuid, status);
        }

        /// <summary>
        /// 通过ParentTaskID获取 子任务列表
        /// </summary>
        /// <param name="parentSubTaskGuid"></param>
        /// <returns></returns>
        public static List<SubTaskInfo> GetSubTasksByParentSubTaskGuid(Guid parentSubTaskGuid, SubTaskStatus? status)
        {
            return BusinessDataProvider.Instance().GetSubTasksByParentSubTaskGuid(parentSubTaskGuid, status);
        }

        /// <summary>
        /// 获取Activity的所有SubTask
        /// </summary>
        /// <param name="activityGuid">活动ID</param>
        /// <returns>SubTaskInfo集合</returns>
        public static List<SubTaskInfo> GetSubTasksByActivityGuid(Guid activityGuid)
        {
            return BusinessDataProvider.Instance().GetSubTasks(activityGuid, null);
        }

        /// <summary>
        /// 获取Activity的所有SubTask
        /// </summary>
        /// <param name="activityGuid">活动ID</param>
        /// <param name="isRecursive">是否需要递归</param>
        /// <returns>SubTaskInfo集合</returns>
        public static List<SubTaskInfo> GetSubTasksByParentSubTaskGuid(Guid activityGuid, SubTaskStatus? status, bool isRecursive)
        {
            List<SubTaskInfo> subTaskList = BusinessDataProvider.Instance().GetSubTasksByParentSubTaskGuid(activityGuid, status);
            if (isRecursive == true&& subTaskList!=null)
            {
                for (int i = 0; i < subTaskList.Count; i++)
                {
                    List<SubTaskInfo> allSubTaskList = null;
                    allSubTaskList= GetSubTasksByParentSubTaskGuid(subTaskList[i].SubTaskGuid, status,isRecursive);
                    if (allSubTaskList != null)
                    {
                        subTaskList.AddRange(allSubTaskList);
                    }
                }
            }

            return subTaskList;
        }

        /// <summary>
        /// 判断一个 子任务 是否为另外一个 子任务 的子任务
        /// </summary>
        /// <returns></returns>
        public static bool IsSubTask(Guid subTaskGuid,Guid parentSubTaskGuid)
        {
            if (parentSubTaskGuid == Guid.Empty)
            {
                return false;
            }

            List<SubTaskInfo> subTaskList = GetSubTasksByParentSubTaskGuid(parentSubTaskGuid,null,true);

            for (int i = 0; i < subTaskList.Count; i++)
            {
                if (subTaskList[i].SubTaskGuid == subTaskGuid)
                {
                    return true;
                }
            }

            return false;
        }

        /// <summary>
        ///  检查待创建的SubTask是否有执行人
        /// </summary>
        /// <param name="subTaskOrganizationIDs">发起SubTask的部门ID数组</param>
        /// <param name="subFlow">发起SubTask的SubFlow</param>
        /// <param name="errorOrganization">存在没有执行人的部门名称</param>
        /// <returns></returns>
        public static bool CheckSubTaskHasExectors(string[] subTaskOrganizationIDs, SubFlowInfo subFlow, out string errorOrganization)
        {
            ActivityPrototypeInfo currentActivityPrototype = ActivityPrototypes.GetFirstActivityPrototypeOfSubFlow(subFlow.WorkflowGuid, subFlow.SubFlowGuid);
            foreach (string subTaskOrganizationID in subTaskOrganizationIDs)
            {
                //WorkItem执行人
                ReadOnlyCollection<IUser> executors = null;

                if (!string.IsNullOrEmpty(currentActivityPrototype.RequiredUserNames))
                {
                    string[] executorUserNames = null;
                    executorUserNames = currentActivityPrototype.RequiredUserNames.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

                    foreach (string userName in executorUserNames)
                    {
                        IUser _user = ChinaCustomsPermissionWrapper.GetUser(userName);
                        if (_user != null)
                            continue;
                    }
                }

                string[] roleArray = null;
                if (!string.IsNullOrEmpty(currentActivityPrototype.RequiredRoles.Trim()))
                {
                    roleArray = currentActivityPrototype.RequiredRoles.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                }
                string[] organizationIDArray = new string[] { subTaskOrganizationID };

                TaskInfo taskInfo = Tasks.GetTaskByWorkflowGuid(subFlow.WorkflowGuid);
                executors = new ReadOnlyCollection<IUser>(ChinaCustomsPermissionWrapper.GetUsers(taskInfo.ApplicationCodeName, roleArray, organizationIDArray));

                if (executors == null || executors.Count == 0)
                {
                    errorOrganization = ChinaCustomsPermissionWrapper.GetOrganization(subTaskOrganizationID).DisplayName;
                    return false;
                }
            }

            errorOrganization = string.Empty;
            return true;
        }


    }
}
